﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using SharePoint.Silverlight.Clock.ControlTemplates.SharePoint.Silverlight.Clock;

namespace SharePoint.Silverlight.Clock.EditorParts
{
    class EditorBase : EditorPart
    {
        #region Const
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SharePoint.Silverlight.Clock/ClockWebPart/EditorParts/EditorPartMain.ascx";
        #endregion

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Control control = Page.LoadControl(_ascxPath);
            control.ID = ID + "EditorPartMain";
            Controls.Add(control);
        }

        public override bool ApplyChanges()
        {
            ClockWebPart wp = this.WebPartToEdit as ClockWebPart;
            EditorPartMain partMain = this.FindControl(ID + "EditorPartMain") as EditorPartMain;

            wp.ClockTitle = partMain.ClockTitle;
            wp.ShowDigitalClock = partMain.ShowDigitalClock;
            wp.ShowSecondHand = partMain.ShowSecondHand;
            wp.ShowMilliHand = partMain.ShowMilliHand;
            wp.ShowTenthsHand = partMain.ShowTenthsHand;
            wp.ClockTimeZoneId = partMain.ClockTimeZoneId;
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            ClockWebPart wp = this.WebPartToEdit as ClockWebPart;
            EditorPartMain partMain = this.FindControl(ID + "EditorPartMain") as EditorPartMain;

            partMain.Initialize();
            
            partMain.ClockTitle = wp.ClockTitle;
            partMain.ShowDigitalClock = wp.ShowDigitalClock;
            partMain.ShowSecondHand = wp.ShowSecondHand;
            partMain.ShowMilliHand = wp.ShowMilliHand;
            partMain.ShowTenthsHand = wp.ShowTenthsHand;
            partMain.ClockTimeZoneId = wp.ClockTimeZoneId;

        }
    }
}
